import java.util.ArrayList;
import java.util.Scanner;



public class ManageDisplay {
	private Scanner sc = new Scanner(System.in); 
	
	public ManageDisplay()
	{
		
	}
	
	public void searhProcess()
	{
		//Type of Search//
		System.out.println("Enter type of search :");
		System.out.println("1. Search By Movie: ");
		System.out.println("2. Search By Cinema: ");
		int choice = sc.nextInt();
		if (choice ==1)
		{
			System.out.println("Enter type of search: ");
			System.out.println("1. Now Showing ");
			System.out.println("2. Coming soon ");
			choice = sc.nextInt();
			if (choice==1)
			{
				System.out.println("List of Now Showing Movie: ");
				for (int i=0; i<MovieApp.movieList.size();i++)
					if (MovieApp.movieList.get(i).getStatus()==0)
					{
						System.out.println("Movie ID  : "+(i+1));
						System.out.println("Movie name: "+MovieApp.movieList.get(i).getMovieName());
					}
			
				searchByMovie();
			}
			else 
			{
				System.out.println("List of Coming Soon Movie: ");
				for (int i=0; i<MovieApp.movieList.size();i++)
					if (MovieApp.movieList.get(i).getStatus()==2)
					{
						System.out.println("Movie ID  : "+(i+1));
						System.out.println("Movie name: "+MovieApp.movieList.get(i).getMovieName());
					}
				
				System.out.println(" Do you want to come back to search process (y/n): ");
				char choice1 = sc.next().charAt(0);
				if (choice1== 'y')
					searhProcess();
			}
		}
		else
			searchByCinema();
	}
	public void searchByMovie()
	{
		// Get the value of Cinema and Movie //
		System.out.println("Enter your choice of Movie: ");
	
		int choiceOfMovie = sc.nextInt()-1;
		
		for (int i=0;i < MovieApp.cineplexList.size();i++)
			System.out.println(i+1+" " +MovieApp.cineplexList.get(i).getCineplexName());
		System.out.println("Enter your choice of Cinema: ");
		
		int choiceOfCineplex = sc.nextInt()-1;
		printShowAndBook(choiceOfCineplex,choiceOfMovie);
	}
	
	public void searchByCinema()
	{
		// Get the value of Cinema and Movie //
		for (int i=0;i < MovieApp.cineplexList.size();i++)
			System.out.println(i+1+" " +MovieApp.cineplexList.get(i).getCineplexName());
		System.out.println("Enter your choice of Cinema: ");
		int choiceOfCineplex = sc.nextInt()-1;
		ArrayList<String> temp = new ArrayList<String>();
		
		// get the List of Movie showing in chosen cinema //
		for (int i=0; i<MovieApp.cineplexList.get(choiceOfCineplex).getShowList().size();i++)
			if (temp.contains(MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getMovieName())==false)
				temp.add(MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getMovieName());
		for (int i=0; i < temp.size();i++)
			System.out.println(i+1+ " " + temp.get(i));
		
		System.out.println("Enter your choice of Movie: ");
		int choiceOfMovie = sc.nextInt()-1;
		
		// check the index of chosen movie in the movie list //
		for (int i=0; i<MovieApp.movieList.size();i++)
			if (MovieApp.movieList.get(i).getMovieName().trim().toLowerCase().equals(temp.get(choiceOfMovie).trim().toLowerCase()))
			{
				choiceOfMovie = i;
				break;
			}
		printShowAndBook(choiceOfCineplex,choiceOfMovie);
	}
	
	public void printShowAndBook(int choiceOfCineplex, int choiceOfMovie)
	{
		int j;
		// check whether the chosen cinema has the chosen movie or not//
		for (j =0; j<MovieApp.cineplexList.get(choiceOfCineplex).getShowList().size();j++)
		if(MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(j).getMovieName().trim().toLowerCase().equals(MovieApp.movieList.get(choiceOfMovie).getMovieName().trim().toLowerCase())==true)
		{
		// print out the show for the chosen movie and chosen cinema//
			for (int i=0; i< MovieApp.cineplexList.get(choiceOfCineplex).getShowList().size();i++)
				if (MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getMovieName().trim().toLowerCase().equals(MovieApp.movieList.get(choiceOfMovie).getMovieName().trim().toLowerCase()))
				{	
					System.out.println("Show ID     : "+(i+1));
					System.out.println("Movie Name  : "+MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getMovieName());
					System.out.println("Show time   : "+MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getTime());
					System.out.println("Cineplex    : "+MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getCineplex());
					System.out.println("Cinema      : "+MovieApp.cineplexList.get(choiceOfCineplex).getShowList().get(i).getCinemaItem().getCinemaName());
				}
			
			System.out.println("Do you want to book show (y/n)? ");
			char choice = sc.next().trim().toLowerCase().charAt(0);
			if (choice == 'y')
			{
				System.out.println("Enter the choice of Show ID : ");
				int choiceOfShow = sc.nextInt()-1;
				ManageBooking bookingItem = new ManageBooking();
				bookingItem.book(choiceOfMovie,choiceOfCineplex,choiceOfShow);
			}
			break;
		}
	
		if (j==MovieApp.cineplexList.get(choiceOfCineplex).getShowList().size()) 
		{
			System.out.println("No Movie!");
			searhProcess();
		}
	
	}
}
